<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学员姓名" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['system:student:add']"-->
      <!--        >新增-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:student:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:student:remove']"
        >删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['system:student:export']"-->
      <!--        >导出-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column
        label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column label="学员姓名" align="center" prop="studentName"/>
      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="260">
        <template slot-scope="scope">
          <el-col :span="1.5">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleShowAchievement(scope.row)"
              v-hasPermi="['system:student:edit']"
            >数据查看
            </el-button>

            <el-button
              type="text"
              icon="el-icon-pie-chart"
              size="mini"
              @click="handleLine(scope.row)"
              v-hasPermi="['system:achievement:export']"
            >折线图
            </el-button>
          </el-col>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:student:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:student:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学员管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学员姓名" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入内容" maxlength="10" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!--学员成绩详情 -->
    <el-dialog :title="titleAchievement" :visible.sync="openAchievement" width="70%" append-to-body>

      <el-form :model="queryParamsAchievement" ref="queryFormAchievement" size="small" :inline="true"
               v-show="showSearchAchievement" label-width="68px">

        <el-form-item label="导入日期" prop="createDate">
          <el-date-picker
            v-model="queryParamsAchievement.createDate"
            style="width: 230px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @keyup.enter.native="handleQueryAchievement"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="考试名称" prop="name">
          <el-input
            v-model="queryParamsAchievement.name"
            placeholder="请输入考试名称"
            clearable
            @keyup.enter.native="handleQueryAchievement"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryAchievement">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQueryAchievement">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdateAchievement"
            v-hasPermi="['system:achievement:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDeleteAchievement"
            v-hasPermi="['system:achievement:remove']"
          >删除
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearchAchievement" @queryTable="getListAchievement"></right-toolbar>
      </el-row>

      <el-table v-loading="loadingAchievement" :data="achievementList"
                @selection-change="handleSelectionChangeAchievement">
        <el-table-column type="selection" width="55" align="center"/>
        <!--      <el-table-column label="id" align="center" prop="id" />-->
        <el-table-column
          fixed="left"
          label="序号"
          type="index"
          width="50">
        </el-table-column>
        <el-table-column label="导入时间" align="center" prop="createTime" width="150px" fixed="left"/>
        <el-table-column label="考试名称" align="center" prop="examName" width="250px" fixed="left"/>
        <el-table-column label="学员姓名" align="center" prop="studentName" width="75" fixed="left"/>
        <el-table-column label="科目一分数" align="center" prop="subjectOne" width="85"/>
        <el-table-column label="科目一速度" align="center" prop="subjectOneSpeed" width="85"/>
        <el-table-column label="科目一评定" align="center" prop="subjectOneEvaluate" width="85"/>
        <el-table-column label="科目二分数" align="center" prop="subjectTwo" width="85"/>
        <el-table-column label="科目二速度" align="center" prop="subjectTwoSpeed" width="85"/>
        <el-table-column label="科目二评定" align="center" prop="subjectTwoEvaluate" width="85"/>
        <el-table-column label="科目三分数" align="center" prop="subjectThree" width="85"/>
        <el-table-column label="科目三速度" align="center" prop="subjectThreeSpeed" width="85"/>
        <el-table-column label="科目三评定" align="center" prop="subjectThreeEvaluate" width="85"/>
        <el-table-column label="科目四分数" align="center" prop="subjectFour" width="85"/>
        <el-table-column label="科目四速度" align="center" prop="subjectFourSpeed" width="85"/>
        <el-table-column label="科目四评定" align="center" prop="subjectFourEvaluate" width="85"/>
        <el-table-column label="科目五分数" align="center" prop="subjectFive" width="85"/>
        <el-table-column label="科目五速度" align="center" prop="subjectFiveSpeed" width="85"/>
        <el-table-column label="科目五评定" align="center" prop="subjectFiveEvaluate" width="85"/>
        <el-table-column label="科目六分数" align="center" prop="subjectSix" width="85"/>
        <el-table-column label="科目六速度" align="center" prop="subjectSixSpeed" width="85"/>
        <el-table-column label="科目六评定" align="center" prop="subjectSixEvaluate" width="85"/>
        <el-table-column label="科目七分数" align="center" prop="subjectSeven" width="85"/>
        <el-table-column label="科目七速度" align="center" prop="subjectSevenSpeed" width="85"/>
        <el-table-column label="科目七评定" align="center" prop="subjectSevenEvaluate" width="85"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="175">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-pie-chart"
              @click="handleRadar(scope.row)"
              v-hasPermi="['system:achievement:edit']"
            >雷达图
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateAchievement(scope.row)"
              v-hasPermi="['system:achievement:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeleteAchievement(scope.row)"
              v-hasPermi="['system:achievement:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="totalAchievement>0"
        :total="totalAchievement"
        :page.sync="queryParamsAchievement.pageNum"
        :limit.sync="queryParamsAchievement.pageSize"
        @pagination="getListAchievement"
      />

      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="handleSelectAchievement">确 定</el-button>-->
        <el-button @click="openAchievementCancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--雷达图 -->
    <el-dialog :title="titleRadar" :visible.sync="openRadar" width="50%" append-to-body>
      <template>
        <div>
          <div ref="chart" id="mainRadar" style="height: 500px;width: 100%;"></div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="exportExcel">导出excel</el-button>
          <el-button @click="cancelRadar">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 添加或修改学员考试成绩管理对话框 -->
    <el-dialog :title="titleAchievementUpdate" :visible.sync="openAchievementUpdate" width="40%" append-to-body>
      <el-form ref="formAchievement" :model="formAchievement" :rules="rulesAchievement" label-width="100px">
        <!--        <el-form-item label="考试id" prop="examId">
                  <el-input v-model="formAchievement.examId" placeholder="请输入考试id"/>
                </el-form-item>
                <el-form-item label="学员id" prop="studentId">
                  <el-input v-model="formAchievement.studentId" placeholder="请输入学员id"/>
                </el-form-item>-->

        <el-row>
          <el-col :span="12">
            <el-form-item label="考试名称" prop="examName">
              <el-input v-model="formAchievement.examName" placeholder="请输入考试名称" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学员姓名" prop="studentName">
              <el-input v-model="formAchievement.studentName" placeholder="请输入学员姓名" :disabled="true"/>
            </el-form-item>
          </el-col>

        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="科目一分数" prop="subjectOne">
              <el-input v-model="formAchievement.subjectOne" placeholder="请输入科目一分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目一速度" prop="subjectOneSpeed">
              <el-input v-model="formAchievement.subjectOneSpeed" placeholder="请输入科目一速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目一评定" prop="subjectOneEvaluate">
              <el-select
                v-model="formAchievement.subjectOneEvaluate"
                placeholder="科目一评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目二分数" prop="subjectTwo">
              <el-input v-model="formAchievement.subjectTwo" placeholder="请输入科目二分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目二速度" prop="subjectTwoSpeed">
              <el-input v-model="formAchievement.subjectTwoSpeed" placeholder="请输入科目二速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目二评定" prop="subjectTwoEvaluate">
              <el-select
                v-model="formAchievement.subjectTwoEvaluate"
                placeholder="科目二评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目三分数" prop="subjectThree">
              <el-input v-model="formAchievement.subjectThree" placeholder="请输入科目三分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目三速度" prop="subjectThreeSpeed">
              <el-input v-model="formAchievement.subjectThreeSpeed" placeholder="请输入科目三速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目三评定" prop="subjectThreeEvaluate">
              <el-select
                v-model="formAchievement.subjectThreeEvaluate"
                placeholder="科目三评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目四分数" prop="subjectFour">
              <el-input v-model="formAchievement.subjectFour" placeholder="请输入科目四分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目四速度" prop="subjectFourSpeed">
              <el-input v-model="formAchievement.subjectFourSpeed" placeholder="请输入科目四速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目四评定" prop="subjectFourEvaluate">
              <el-select
                v-model="formAchievement.subjectFourEvaluate"
                placeholder="科目四评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目五分数" prop="subjectFive">
              <el-input v-model="formAchievement.subjectFive" placeholder="请输入科目五分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目五速度" prop="subjectFiveSpeed">
              <el-input v-model="formAchievement.subjectFiveSpeed" placeholder="请输入科目五速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目五评定" prop="subjectFiveEvaluate">
              <el-select
                v-model="formAchievement.subjectFiveEvaluate"
                placeholder="科目五评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="科目六分数" prop="subjectSix">
              <el-input v-model="formAchievement.subjectSix" placeholder="请输入科目六分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目六速度" prop="subjectSixSpeed">
              <el-input v-model="formAchievement.subjectSixSpeed" placeholder="请输入科目六速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目六评定" prop="subjectSixEvaluate">
              <el-select
                v-model="formAchievement.subjectSixEvaluate"
                placeholder="科目六评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="科目七分数" prop="subjectSeven">
              <el-input v-model="formAchievement.subjectSeven" placeholder="请输入科目七分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目七速度" prop="subjectSevenSpeed">
              <el-input v-model="formAchievement.subjectSevenSpeed" placeholder="请输入科目七速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目七评定" prop="subjectSevenEvaluate">
              <el-select
                v-model="formAchievement.subjectSevenEvaluate"
                placeholder="科目七评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAchievement">确 定</el-button>
        <el-button @click="cancelAchievement">取 消</el-button>
      </div>
    </el-dialog>


    <!--折线图 -->
    <el-dialog :title="titleLine" :visible.sync="openLine" width="70%" append-to-body>
      <template>
        <el-form :model="queryParamsAchievementLine" ref="queryFormAchievementLine" size="small" :inline="true"
                 v-show="showSearchAchievementLine" label-width="68px">
          <el-form-item label="导入日期" prop="createDate">
            <el-date-picker
              :clearable="false"
              v-model="queryParamsAchievementLine.createDate"
              style="width: 230px"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @keyup.enter.native="handleQueryAchievementLine">
            </el-date-picker>
          </el-form-item>

          <el-form-item label="科目编号" prop="subjectNo">
            <el-select
              v-model="queryParamsAchievementLine.subjectNo"
              placeholder="科目编号"
            >
              <el-option
                v-for="dict in dict.type.subject_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryAchievementLine">搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQueryAchievementLine">重置</el-button>
          </el-form-item>
        </el-form>
        <div>
          <div ref="chartLine" id="mainLine" style="height: 500px;width:100%;"></div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="exportLineExcel">导出excel</el-button>
          <el-button type="info" @click="cancelLine">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import {listStudent, getStudent, delStudent, addStudent, updateStudent} from "@/api/system/student";
import {
  achievementLine,
  delAchievement,
  getAchievement,
  listAchievement,
  radar,
  updateAchievement
} from "@/api/system/achievement";
import ExcelJS from "exceljs";
import * as echarts from "echarts";
import {barGraph} from "@/api/system/manage";
import dayjs from "dayjs";

export default {
  name: "Student",
  dicts: ['subject_evaluate', 'subject_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 遮罩层
      loadingAchievement: true,
      loadingAchievementLine: true,
      // 选中数组
      ids: [],
      achievementIds: [],

      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      showSearchAchievement: true,
      showSearchAchievementLine: true,

      // 总条数
      total: 0,
      totalAchievement: 0,
      // 学员管理表格数据
      studentList: [],
      achievementList: [],
      examNameList: [],
      subjectScoreList: [],
      subjectSpeedList: [],
      // 弹出层标题
      title: "",
      titleLine: "",
      titleAchievement: "",
      titleAchievementUpdate: "",
      // 柱状图标题
      titleRadar: "",
      // 是否显示弹出层
      open: false,
      openLine: false,
      openAchievement: false,
      openAchievementUpdate: false,
      // 柱状图
      openRadar: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: null,
        studentNo: null,
      },
      queryParamsAchievement: {
        pageNum: 1,
        pageSize: 10,
        examId: null,
        examName: null,
        name: null,
        studentId: null,
        studentName: null,
        createTime: null,
        createDate: [],
      },
      queryParamsAchievementLine: {
        examId: null,
        examName: null,
        name: null,
        studentId: null,
        studentName: null,
        createTime: null,
        subjectNo: "1",
        createDate: [],
      },
      selectedStudent: {},
      // 表单参数
      form: {},
      // 表单参数
      formAchievement: {},
      // 表单校验
      rules: {
        studentName: [
          {required: true, message: "学员名字不能为空", trigger: "blur"}
        ],
      },
      rulesAchievement: {
        examId: [
          {required: true, message: "考试id不能为空", trigger: "blur"}
        ],
        studentId: [
          {required: true, message: "学员id不能为空", trigger: "blur"}
        ]
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学员管理列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        studentName: null,
        studentNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员数据项？').then(function () {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    },

    /** 查询学员成绩列表 */
    getListAchievement() {
      this.loadingAchievement = true;
      listAchievement(this.queryParamsAchievement).then(response => {
        this.achievementList = response.rows;
        this.totalAchievement = response.total;
        this.loadingAchievement = false;
      });
    },
    // 表单重置
    resetAchievement() {
      this.formAchievement = {
        id: null,
        examId: null,
        examName: null,
        studentId: null,
        studentName: null,
        subjectOne: null,
        subjectOneSpeed: null,
        subjectOneEvaluate: null,
        subjectTwo: null,
        subjectTwoSpeed: null,
        subjectTwoEvaluate: null,
        subjectThree: null,
        subjectThreeSpeed: null,
        subjectThreeEvaluate: null,
        subjectFour: null,
        subjectFourSpeed: null,
        subjectFourEvaluate: null,
        subjectFive: null,
        subjectFiveSpeed: null,
        subjectFiveEvaluate: null,
        subjectSix: null,
        subjectSixSpeed: null,
        subjectSixEvaluate: null,
        subjectSeven: null,
        subjectSevenSpeed: null,
        subjectSevenEvaluate: null,
        createBy: null,
        createTime: null,
        createDate: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("formAchievement");
    },
    /** 搜索按钮操作 */
    handleQueryAchievement() {
      this.queryParamsAchievement.pageNum = 1;
      this.getListAchievement();
    },
    /** 重置按钮操作 */
    resetQueryAchievement() {
      this.resetForm("queryFormAchievement");
      this.handleQueryAchievement();
    },
    // 多选框选中数据
    handleSelectionChangeAchievement(selection) {
      this.achievementIds = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    handleShowAchievement(row) {

      var now = dayjs()
      var endDate = now.format('YYYY-MM-DD');
      var startDate = now.subtract(30, 'day').format('YYYY-MM-DD');
      this.queryParamsAchievement.createDate = [startDate, endDate];


      this.openAchievement = true
      this.queryParamsAchievement.studentId = row.id
      this.titleAchievement = row.studentName + "的成绩详情"
      this.achievementList = []
      listAchievement(this.queryParamsAchievement).then(response => {
        this.achievementList = response.rows;
        this.totalAchievement = response.total;
        this.loadingAchievement = false;
      });
    },

    /** 修改按钮操作 */
    handleUpdateAchievement(row) {
      this.resetAchievement();
      const id = row.id || this.achievementIds
      getAchievement(id).then(response => {
        this.formAchievement = response.data;
        this.openAchievementUpdate = true;
        this.titleAchievementUpdate = "修改学员成绩";
      });
    },
    /** 删除按钮操作 */
    handleDeleteAchievement(row) {
      const ids = row.id || this.achievementIds;
      this.$modal.confirm('是否删除学员成绩').then(function () {
        return delAchievement(ids);
      }).then(() => {
        this.getListAchievement();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

    // 取消按钮
    cancelAchievement() {
      this.openAchievementUpdate = false;
      this.resetQueryAchievement()
    },

    openAchievementCancel() {
      this.openAchievement = false;
      this.resetQuery()
    },

    /** 提交按钮 */
    submitFormAchievement() {
      this.$refs["formAchievement"].validate(valid => {
        if (valid) {
          if (this.formAchievement.id != null) {
            updateAchievement(this.formAchievement).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openAchievementUpdate = false;
              this.getListAchievement();
            });
          } else {
            addStudent(this.formAchievement).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openAchievementUpdate = false;
              this.getListAchievement();
            });
          }
        }
      });
    },

    /** 雷达图按钮操作 */
    handleRadar(row) {
      this.openRadar = true;
      this.titleRadar = row.name;
      this.$nextTick(() => {
        this.myCharts(row)
      })
    },

    // 取消按钮
    cancelRadar() {
      this.openRadar = false;
      this.resetQueryAchievement();
    },

    exportExcel() {
      const workbook = new ExcelJS.Workbook(); // 创建工作簿
      const worksheet = workbook.addWorksheet('雷达图可视化分析'); // 添加工作表

      const chart = echarts.getInstanceByDom(this.$refs.chart) // 获取图表实例
      const base64Image = chart.getDataURL({
        pixelRatio: 2, // 导出图片的分辨率比例，默认为1，即图片的分辨率为屏幕分辨率的一倍
        backgroundColor: '#fff' // 导出图片的背景色
      })
      let image = workbook.addImage({ // 添加图片
        base64: base64Image, // 图片的base64编码
        extension: 'png'  // 图片的扩展名
      });
      worksheet.addImage(image, 'A1:Q34'); // 将图片添加到工作表中
      workbook.xlsx.writeBuffer().then(function (buffer) { // 生成excel文件的二进制数据
        saveAs.saveAs(new Blob([buffer], {  // 生成Blob对象
          type: 'application/octet-stream'  // 指定文件的MIME类型
        }), '雷达图可视化分析.xlsx');  // 指定文件名
      });
    },
    /** 雷达图按钮操作 */
    myCharts(row) {
      radar(row.id).then(response => {
        const value = response.data;
        const text = row.studentName + row.examName + "雷达图可视化分析";

        // 检测是否已经存在echarts实例，如果不存在，则不再去初始化
        let myChart = echarts.getInstanceByDom(document.getElementById('mainRadar'))
        // 如果为空 则正常进行渲染 反之 不再进行初始化
        if (myChart == null) {
          myChart = echarts.init(document.getElementById('mainRadar'))
        }

        const option = {
          title: {
            text: text
          },
          radar: {
            // shape: 'circle',
            indicator: [
              {name: '科目一', max: 150},
              {name: '科目二', max: 150},
              {name: '科目三', max: 150},
              {name: '科目四', max: 150},
              {name: '科目五', max: 150},
              {name: '科目六', max: 150},
              {name: '科目七', max: 150}
            ],
            textStyle: {
              fontSize: 16, // 设置字体大小
              // 其他文本样式设置，例如字体颜色，字体粗细等
              // color: 'red',
              // fontWeight: 'bold'
            },
            axisLine: {
              lineStyle: {
                color: '#16a085' // 设置雷达图的轴线颜色为红色
              },
            },
            splitLine: {
              lineStyle: {
                color: '#c282d9' // 设置雷达图的分割线颜色为蓝色
              },
            }
          },
          series: [
            {
              name: 'Budget vs spending',
              type: 'radar',
              label: {
                color: '#ff4757',
                fontSize: 19, // 设置字体大小
                show: true,
                formatter: function (item) {
                  return item.value;
                }
              },
              data: [
                {
                  value: value
                }
              ]
            }
          ]
        };
        myChart.setOption(option);
        // 根据页面大小自动响应图表大小
        // window.addEventListener("resize", function () {
        //   myChart.resize();
        // });
      });
    },

    //===========================
    /** 重置按钮操作 */
    resetQueryAchievementLine() {
      this.resetForm("queryFormAchievementLine");
      this.handleQueryAchievement();
    },

    /** 折线图按钮操作 */
    handleLine(row) {

      var now = dayjs()
      var endDate = now.format('YYYY-MM-DD');
      var startDate = now.subtract(7, 'day').format('YYYY-MM-DD');
      this.queryParamsAchievementLine.createDate = [startDate, endDate];

      this.openLine = true;
      this.titleLine = row.studentName;
      this.selectedStudent = row
      this.$nextTick(() => {
        this.myLineCharts(row)
      })
    },
    // 取消按钮
    cancelLine() {
      this.openLine = false;
      this.resetQuery();
    },
    exportLineExcel() {
      const workbook = new ExcelJS.Workbook(); // 创建工作簿
      const worksheet = workbook.addWorksheet('折线图可视化分析'); // 添加工作表

      const chart = echarts.getInstanceByDom(this.$refs.chartLine) // 获取图表实例
      const base64Image = chart.getDataURL({
        pixelRatio: 2, // 导出图片的分辨率比例，默认为1，即图片的分辨率为屏幕分辨率的一倍
        backgroundColor: '#fff' // 导出图片的背景色
      })
      let image = workbook.addImage({ // 添加图片
        base64: base64Image, // 图片的base64编码
        extension: 'png'  // 图片的扩展名
      });
      worksheet.addImage(image, 'A1:Z51'); // 将图片添加到工作表中
      workbook.xlsx.writeBuffer().then(function (buffer) { // 生成excel文件的二进制数据
        saveAs.saveAs(new Blob([buffer], {  // 生成Blob对象
          type: 'application/octet-stream'  // 指定文件的MIME类型
        }), '折线图可视化分析.xlsx');  // 指定文件名
      });
    },

    /** 折线图按钮操作 */
    myLineCharts(row) {
      this.loadingAchievementLine = true;
      this.queryParamsAchievementLine.studentId = row.id;
      this.queryParamsAchievementLine.studentName = row.studentName;
      let dictMap = {}
      for (let i = 0; i < this.dict.type.subject_no.length; i++) {
        let item = this.dict.type.subject_no[i];
        dictMap[item.value] = item.label;
      }
      let subjectName = dictMap[this.queryParamsAchievementLine.subjectNo]
      let dataNameList = [];
      dataNameList.push(subjectName + '分数')
      dataNameList.push(subjectName + '速度')
      achievementLine(this.queryParamsAchievementLine).then(response => {

        this.examNameList = response.data.examNameList;
        this.subjectScoreList = response.data.subjectScoreList;
        this.subjectSpeedList = response.data.subjectSpeedList;
        const text = this.selectedStudent.studentName + subjectName + "折线图可视化分析";

        // 检测是否已经存在echarts实例，如果不存在，则不再去初始化
        let myChartLine = echarts.getInstanceByDom(document.getElementById('mainLine'))
        // 如果为空 则正常进行渲染 反之 不再进行初始化
        if (myChartLine == null) {
          myChartLine = echarts.init(document.getElementById('mainLine'))
        }

        const option = {
          title: {
            text: text
          },
          tooltip: {
            trigger: 'axis'
          },
          legend: {
            data: dataNameList
          },
          grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: this.examNameList,
            axisLabel: {
              // 旋转角度
              rotate: 45
            }
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: subjectName + '分数',
              type: 'line',
              data: this.subjectScoreList,
              label: {
                show: true // 显示数据点的值
              }
            },
            {
              name: subjectName + '速度',
              type: 'line',
              data: this.subjectSpeedList,
              label: {
                show: true // 显示数据点的值
              }
            }
          ]
        };
        myChartLine.setOption(option);
        // 根据页面大小自动响应图表大小
        // window.addEventListener("resize", function () {
        //   myChartLine.resize();
        // });
        this.loadingAchievementLine = false;
      })
    },
    /** 折线图搜索按钮操作 */
    handleQueryAchievementLine() {
      this.getListAchievementLine();
    },
    /** Line */
    getListAchievementLine() {
      this.myLineCharts(this.selectedStudent)
    },
  }
};
</script>
